package org.cellstore.app;

import javax.swing.SwingUtilities;
import org.apache.log4j.Logger;
import org.cellstore.controlador.CellStoreController;
import org.cellstore.gui.MDIPrincipal;
import org.palituxd.tools.justonelock.JustOneLock;
import org.starcell.spring.Spring;

/**
 * Hello world!
 *
 */
public class App {

    private String[] args;
    private static String APLICACION = Etiqueta.getEtiqueta("APP.nombre");
    private static String PROCESO = Etiqueta.getEtiqueta("APP.proceso");

    public App(String[] args) {
        this.args = args;
        log.info(APLICACION);
        log.info("BIENVENIDO");
    }

    private void correrApp() {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                lookAndFeel();
                CellStoreController controller = Spring.getInstance().getController();
                MDIPrincipal dMIPrincipal = new MDIPrincipal();
                dMIPrincipal.setVisible(true);
            }
        });
    }

    public static void lookAndFeel() {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                System.out.println(info.getName());
//                String laf = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
//                javax.swing.UIManager.setLookAndFeel(laf);
////                if ("Nimbus".equals(info.getName())) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(App.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(App.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(App.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(App.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
    }

    public static void main(String[] args) {
        JustOneLock ua = new JustOneLock(PROCESO);
        App app = null;
        if (ua.isAppActive()) {
            log.info("PROCESO " + PROCESO + " YA SE ENCUENTRA EJECUTADO");
            System.exit(1);
        } else {
            log.info("INICIANDO PROCESO " + PROCESO);
            app = new App(args);
            app.correrApp();
        }
    }
    static Logger log = Logger.getLogger(App.class);
}
